package com.fa.wisdom.ui;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.adapter.FoundRecycleAdapter;
import com.fa.wisdom.adapter.QiuzhuAdapter;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.model.MsgEntity;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.SpUtils;
import com.fa.wisdom.utils.TitleBuilder;
import com.tianyi.putorefresh.BaseBothListener;
import com.tianyi.putorefresh.PullToBothLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class JKDataListActivity extends BaseActivity {
    RecyclerView recyclerView;
    View statusBarPlace;
    PullToBothLayout pullToRefresh_layout;
    private FoundRecycleAdapter foundRecycleAdapter = null;
    List<MsgEntity> msgEntities = new ArrayList<>();
    private int page = 1;
    private int id;
    private boolean isRefresh = false;
    private String url;
    String detail;
    int type;
    @Override
    public void setListen() {
        new TitleBuilder(this).setTitleText(detail)
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
        pullToRefresh_layout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                page = 1;
                loadData();
                isRefresh = true;
                pullToRefresh_layout.finishRefresh();
            }

            @Override
            public void loadMore() {
                page++;
                isRefresh = false;
                loadData();
                pullToRefresh_layout.finishLoadMore();
            }
        });
    }
    //加载数据
    private void loadData() {
        LoadingDialog dialog = Loading.getInstance().onShowLargeLoading("加载中...");
        String token = SpUtils.getInstance().decodeString(Constant.TOKEN, "");
        dialog.showInActivity(this);
        OkHttpUtils.get().url(url)
                .addParams("token", token)
                .addParams("device_group_id", String.valueOf(SpUtils.getInstance().decodeInt("group_id", 0)))
                .addParams("page", String.valueOf(page)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                dialog.dismiss();
                pullToRefresh_layout.finishLoadMore();
                pullToRefresh_layout.finishRefresh();
            }

            @Override
            public void onResponse(String response, int id) {
                dialog.dismiss();
                JSONObject jsonObject = JSON.parseObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                pullToRefresh_layout.finishLoadMore();
                pullToRefresh_layout.finishRefresh();
                if (jsonArray != null && jsonArray.size() > 0) {
                    if (isRefresh)
                        msgEntities.clear();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        MsgEntity msgEntity = new MsgEntity();
                        switch (type){
                            case 1://血糖
                                msgEntity.setContent(jsonObject1.getString("alarmcode"));
                                break;
                            case 2://心率
                                msgEntity.setContent(jsonObject1.getString("heart"));
                                break;
                            case 3://血氧
                                msgEntity.setContent(jsonObject1.getString("blood_oxygen"));
                                break;
                            case 4://体温
                                msgEntity.setContent(jsonObject1.getString("temperature"));
                                break;
                            case 5://血脂
                                msgEntity.setContent(jsonObject1.getString("blood_fat"));
                                break;
                        }

                        msgEntity.setTime(jsonObject1.getString("create_time"));
                        msgEntity.setTitle(jsonObject1.getString(detail));
                        msgEntities.add(msgEntity);
                    }
                }
                foundRecycleAdapter.notifyDataSetChanged();
            }
        });


    }
    private void initAdapter() {
        foundRecycleAdapter = new FoundRecycleAdapter(msgEntities);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(foundRecycleAdapter);
    }
    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_jkdata_list;
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void initView() {
        type =getIntent().getIntExtra("type",1);
        detail="健康数据";
        switch (type){
            case 1://血糖
                detail="血糖";
                url="https://br.feiaikeji.cn/index.php/api/device/blood_sugar";
                break;
            case 2://心率
                detail="心率";
                url="https://br.feiaikeji.cn/index.php/api/device/heart_rate";
                break;
            case 3://血氧
                detail="血氧";
                url="https://br.feiaikeji.cn/index.php/api/device/blood_oxygen";
                break;
            case 4://体温
                detail="体温";
                url="https://br.feiaikeji.cn/index.php/api/device/temperature";
                break;
            case 5://血脂
                detail="血脂";
                url="https://br.feiaikeji.cn/index.php/api/device/blood_fat";
                break;
        }
        recyclerView=findViewById(R.id.recycle);
        pullToRefresh_layout = findViewById(R.id.pullRefresh);
        initAdapter();
        loadData();
    }
    @Override
    public void initData() {

    }
}
